package experiment2.springbootautoconfigdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@SpringBootApplication
public class SpringBootAutoconfigDemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringBootAutoconfigDemoApplication.class, args);
    }

    /**
     * 自定义一个事件发布器ApplicationEventMulticaster
     * 自定义一个SimpleApplicationEventMulticaster，并设置线程池，以实现使用线程池异步发布事件
     * @param taskExecutor
     * @return
     */
    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor taskExecutor){
        SimpleApplicationEventMulticaster eventMulticaster=new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExecutor);
        return eventMulticaster;
    }

    @Bean
    public ApplicationListener<ApplicationReadyEvent> listener(){
        return event -> {
            System.out.println("自定义事件监听器 ApplicationReadyEvent");
        };
    }
}
